package standart;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

public class CertificationCenter {
	int port;

	public CertificationCenter(int port) {
		this.port = port;
	}

	public void CheckCertificate() throws IOException, SQLException,
			SAXException, ParserConfigurationException {
		while(true){
		try (ServerSocket servSock = new ServerSocket(port)) {
			Socket socket = servSock.accept();

			InputStream sin = socket.getInputStream();
			OutputStream sout = socket.getOutputStream();

			DataInputStream in = new DataInputStream(sin);
			DataOutputStream out = new DataOutputStream(sout);

				String line = in.readUTF();
				System.out.println("Client send to the certificate server: "
						+ line);
				List xml = new XMLParse()
						.ParseForConnection("configFiles\\DbConfig.xml");
				
				
				DatabaseConnect connectDb = new DatabaseConnect(
						(String) xml.get(0), (String) xml.get(1),
						(String) xml.get(2));
				
				Connection conn = connectDb.AsquireData();
				String res = connectDb.CheckCert(conn, line);
				
				out.writeUTF(res);
				out.flush();
				
				// TODO check the certificate in the different class
			}
		}
	}
		
	}


